<?php


namespace app\logic;

use think\facade\Db;

class ArticleHelper
{
    public static function load_article_category($pid = 0, $checked_ids = []) {


        $list = DataHelper::select('tb_article_category', [
            'pid' => $pid,
            'status' => 0,
        ], [ 'order_by' => 'idx desc' ]);

        foreach ($list as &$item) {
            if (in_array($item['id'], $checked_ids)) {
                $item['checked'] = true;
            }
        }

        foreach ($list as &$item) {
            $children = self::load_article_category($item['id']);
            $item['children'] = $children;
        }

        return $list;
    }

    public static function get_article_category_as_combobox() {
        $rst = [];

        $list_1 = DataHelper::select('tb_article_category', [
            'pid' => 0,
            'status' => 0,
        ], [ 'order_by' => 'idx desc' ]);

        foreach ($list_1 as $item1) {
            $rst[] = [ 'id' => $item1['id'], 'title' => $item1['title'] ];

            $list_2 = DataHelper::select('tb_article_category', [
                'pid' => $item1['id'],
                'status' => 0,
            ], [ 'order_by' => 'idx desc' ]);

            foreach ($list_2 as $item2) {
                $rst[] = [ 'id' => $item2['id'], 'title' => $item1['title'].' / '.$item2['title'] ];

            }

        }


        return $rst;
    }
}